﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Web.BusinessInformation.Example
{
    public partial class GridView_AutoColumn : System.Web.UI.Page
    {
        private static DataTable dt;
        private static readonly string [] value=new string[]
            {
                "天","第","啊","是","厂","非","费","把","玲","A","B","C","D"
            };
        protected void Page_Load(object sender, EventArgs e)
        { 
            if (dt == null)
            {
                dt = CreateTable();
            }
            gvTest.DataSource = dt;
            gvTest.DataBind(); 
           
        }

        public string SetValue(string type)
        {
            bool flag = false; 
            var currentIndex = gvTest.FocusedRowIndex;
            var NextIndex = currentIndex + 1;
            var MaxIndex=gvTest.VisibleRowCount;
            

            return flag.ToString();
        }

        private DataTable CreateTable()
        {

            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("序号", Type.GetType("System.String")));
            dt.Columns.Add(new DataColumn("用户号", Type.GetType("System.String")));
            dt.Columns.Add(new DataColumn("用户名", Type.GetType("System.String")));
            dt.Columns.Add(new DataColumn("描述值", Type.GetType("System.String")));

            for (int i = 0; i < 1001; i++)
            {
                DataRow dr = dt.NewRow();
                dr["序号"] = (i + 1).ToString();
                dr["用户号"] = (500000 + i).ToString();
                dr["用户名"] = CreateName();
                dr["描述值"] = CreateName();
                dt.Rows.Add(dr);
            }


            return dt;
        }

        private string CreateName()
        {
            int maxIndex = value.Length - 1;
            StringBuilder _builder = new StringBuilder();
            Random random=new Random();
            for (int i = 0; i < 10; i++)
            {
                int index = random.Next(0, maxIndex);
                _builder.Append(value[index]);
            } 
            return _builder.ToString();
        }

        protected void gvTest_FocusedRowChanged(object sender, EventArgs e)
        {
            var valueIndex=gvTest.FocusedRowIndex;
           DataRow dr=   gvTest.GetDataRow(valueIndex);
            if (null != dr)
            {
                txtXuHao.Text = dr["序号"].ToString();
                txtYongHuH.Text = dr["用户号"].ToString();
                txtYongHuM.Text = dr["用户名"].ToString();
                txtMiaoShuZ.Text = dr["描述值"].ToString();
            }
        }
    }
}